/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2003-2007 Philippe Durand <draekz@gmail.com>
 * Copyright (C) 2007 Ben Motmans <ben.motmans@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.Collections.Generic;

using Gtk;
using Glade;

using Galaxium.Core;
using Galaxium.Gui;
using Galaxium.Gui.GtkGui;

namespace Galaxium.Protocol.Msn.GtkGui
{
	public sealed class NewContactDialog
	{
		[Widget ("NewContactDialog")]
		private Dialog _dialog = null;
		[Widget ("labelContact")]
		private Label _labelContact = null;
		[Widget ("entryAlias")]
		private Entry _entryAlias = null;
		[Widget ("boxGroup")]
		private HBox _boxGroup = null;
		[Widget ("imgDialog")]
		private Image _imgDialog = null;
		//[Widget ("btnAdd")]
		//private Button _btnAdd = null;
		
		public string Passport { get { return _passport; } }
		public string Nickname { get { return _entryAlias.Text; } }
		public string SelectedGroupID { get { return GetSelectedGroupID(); } }
		
		private ComboBox _comboGroups;
		private ListStore _storeGroups;
		private MsnSession _session;
		private string _passport;
		
		public NewContactDialog(MsnSession session, string passport)
		{
			_passport = passport;
			_session = session;
			
			XML gxml = new XML (GladeUtility.GetGladeResourceStream (typeof (NewContactDialog).Assembly, "ReverseContactDialog.glade"), null, null);
			gxml.Autoconnect (this);
			
			_dialog.Icon = IconUtility.GetIcon ("galaxium-add", IconSizes.Small);
			_imgDialog.FromPixbuf = IconUtility.GetIcon ("galaxium-add-contact", IconSizes.Large);
			
			 CellRendererText renderer = new CellRendererText();
			
			_labelContact.Text = passport;
			
			_storeGroups = new ListStore (typeof (string), typeof (string));
			_comboGroups = new ComboBox ();
			_comboGroups.Model = _storeGroups;
			_comboGroups.PackStart(renderer, false);
			_comboGroups.SetCellDataFunc(renderer, TextCellDataFunc);
			
			_comboGroups.AddAttribute(renderer, "text", 1);
			
			_boxGroup.PackStart(_comboGroups, true, true, 0);
			
			lock (_session.Groups)
			{
				foreach (MsnGroup group in _session.Groups)
					_storeGroups.AppendValues(group.UniqueIdentifier, group.Name);
			}
			
			_comboGroups.Active = 0;
			
			_boxGroup.ShowAll();
			_dialog.ShowAll();
		}
		
		public int Run()
		{
			return _dialog.Run();
		}
		
		public void Destroy()
		{
			_dialog.Hide();
			_dialog.Destroy();
		}
		
		private string GetSelectedGroupID()
		{
			TreeIter iter = TreeIter.Zero;
			string guid = "";
			
			if (_comboGroups.GetActiveIter(out iter))
				guid = (string)_storeGroups.GetValue(iter, 0);
			
			return(guid);
		}
		
		private static CellLayoutDataFunc TextDataFunction = null;
		
		private static void TextDataFunc(CellLayout cell_layout, CellRenderer cell, TreeModel tree_model, TreeIter iter)
		{
			CellRendererText text = (CellRendererText)cell;
			//text.Xpad = 3;
			text.Width = 150;
			text.Ellipsize = Pango.EllipsizeMode.End;
		}
		
		private static CellLayoutDataFunc TextCellDataFunc
		{
			get
			{
				if (TextDataFunction == null)
					TextDataFunction = new CellLayoutDataFunc(TextDataFunc);
				return(TextDataFunction);
			}
		}
	}
}